<?php

use console\db\channels\TransferTableMigration;
use yii\db\Query;

/**
 * 会员卡办卡订单转移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m310030_000008_t_vipcard_card_order extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public $sourceDb = 'olddb';

    /**
     * 是否先清理本地数据表
     */
    public $beforeTruncateTargetTable = false;

    /**
     * @inheritdoc
     */
    public function buildQuery() {
        return (new Query())->from('{{%' . $this->sourceTable . '}}')->where(['>', 'orderid', 73791]);
    }

    /**
     * @inheritdoc
     */
    public function filterData(&$data) {
        if (!parent::validateTransfer($data)) {
            return false;
        }
        
        return true;
    }

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'card_order' => 'trade_vipcard',
            ],
            'columns' => [
                'user_id' => 'uid',
                'type_id' => function() {
                    return 3;
                },
                'order_no' => function ($data) {
                    $time = $data['inputtime'];
                    if (!$time) {
                        $time = $data['updatetime'];
                    }
                    $date = date('Ymd', $time);
                    $rules = [
                        'from' => '0',
                        'paymode' => 1,
                        'date' => substr($date, 2),
                        'sec' => str_pad($time - strtotime($date), 5, 0, STR_PAD_LEFT),
                        'micro' => str_pad(rand(10, 99999), 5, 0, STR_PAD_LEFT),
                    ];
                    return implode($rules);
                },
                'price',
                'express_price' => 'shipping_fee',
                'expired_at' => 'orderid',
                'express_code' => 'shipping_code',
                'express_time' => 'shippingtime',
                'status',
                'created_at' => function($data) {
                    if ($data['inputtime']) {
                        return $data['inputtime'];
                    }
                    return $data['updatetime'];
                },
                'updated_at' => 'updatetime',
            ]
        ];
    }

}
